package com.walkline.screen;

import java.util.Vector;

import localization.WeiboSDKResource;
import net.rim.device.api.ui.Color;
import net.rim.device.api.ui.UiApplication;
import net.rim.device.api.ui.container.MainScreen;
import net.rim.device.api.ui.decor.BackgroundFactory;

import com.walkline.util.Function;
import com.walkline.util.container.ForegroundManager;
import com.walkline.util.container.TestStatusListStyleButtonSet;
import com.walkline.util.network.WorkQueue;
import com.walkline.util.ui.StatusListStyleButtonField;
import com.walkline.util.ui.TestStatusListStyleButtonField;
import com.walkline.weibo.WeiboSDK;
import com.walkline.weibo.inf.Status;
import com.walkline.weibo.inf.StatusWrapper;

public class ShowTestStatusesMainScreen extends MainScreen implements WeiboSDKResource
{
	private WeiboSDK _weibo;
	private WorkQueue _queue = new WorkQueue(2);
	private ForegroundManager _foreground = new ForegroundManager(0);
	private TestStatusListStyleButtonSet _listSet = new TestStatusListStyleButtonSet();
	private TestStatusListStyleButtonField _item;

	private static int _lastMovementTime = 0;

	public ShowTestStatusesMainScreen(WeiboSDK weibo, final StatusWrapper statusWrapper)
	{
		super(NO_VERTICAL_SCROLL | USE_ALL_HEIGHT | NO_SYSTEM_MENU_ITEMS);

		if (statusWrapper == null) {return;}

		getMainManager().setBackground(BackgroundFactory.createSolidBackground(Color.BLACK));

		_weibo = weibo;

		_listSet.setMargin(0, -1, 0, -1);

		UiApplication.getUiApplication().invokeLater(new Runnable()
		{
			public void run() {refreshContents(statusWrapper);}
		});
	}

	private void refreshContents(final StatusWrapper statusWrapper)
	{
		if (statusWrapper != null)
		{
			Vector contents = statusWrapper.getStatuses();

			try {
				if (contents.size() > 0)
				{
					if (_foreground.getManager() == null) {add(_foreground);}

					Status status = null;
					for (int i=0; i<contents.size(); i++)
					{
						status = (Status) contents.elementAt(i);
						_item = new TestStatusListStyleButtonField(_weibo.getAppConfig(), status);
						_listSet.add(_item);
					}
				}

				if (_listSet.getManager() != _foreground) {_foreground.add(_listSet);}
			} catch (Exception e) {
				Function.errorDialog(e.toString());
			}
		}
	}

    protected boolean navigationMovement(int dx, int dy, int status, int time)
    {
    	if (_listSet.getFieldCount() > 0 && _listSet.getFieldWithFocus() instanceof StatusListStyleButtonField)
    	{
    		StatusListStyleButtonField item = (StatusListStyleButtonField) _listSet.getFieldWithFocus();

    		if (item != null)
    		{
            	if (Math.abs(dx) < Math.abs(dy))
            	{
            		if (time - _lastMovementTime > 300)
            		{
            			if (dy < 0)
            			{
            				if (_foreground.getVerticalScroll() > item.getContentTop())
        	    			{
            					int step = _foreground.getVerticalAdjustment().getPageStepSize();
            					_foreground.getVerticalAdjustment().setPageStep(_foreground.getVerticalScroll() - item.getContentTop());
            					_foreground.getVerticalAdjustment().pageStepDown();
            					_foreground.getVerticalAdjustment().setPageStep(step);
        	    			} else {
        	    				_lastMovementTime = time;
        	    				return super.navigationMovement(dx, dy, status, time);
        	    			}
            			} else if (dy > 0) {
            				if (item.getContentHeight() < _listSet.getVisibleHeight())
            				{
            					_lastMovementTime = time;
            					return super.navigationMovement(dx, dy, status, time);
            				} else {
            	    			if (_foreground.getVerticalScroll() == item.getContentTop())
            	    			{
                					int step = _foreground.getVerticalAdjustment().getPageStepSize();
                					_foreground.getVerticalAdjustment().setPageStep(item.getContentHeight() - _foreground.getVisibleHeight());
                					_foreground.getVerticalAdjustment().pageStepUp();
                					_foreground.getVerticalAdjustment().setPageStep(step);
            	    			} else {
            	    				_lastMovementTime = time;
            	    				return super.navigationMovement(dx, dy, status, time);
            	    			}
            				}
            			}
            		}
            	}

    			_lastMovementTime = time;
    			return true;
    		}
    	}

    	return super.navigationMovement(dx, dy, status, time);
    }

    public boolean onClose()
    {
    	_queue.removeAll();

    	return super.onClose();
    }

    protected boolean onSavePrompt() {return true;}
}